package com.flex.core.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.flex.core.utils.ValidateCode;


/**
 * 生成验证码的servlet，需要在web.xml中进行配置
 * 
 *  <servlet>
 *		<servlet-name>checkservlet</servlet-name>
 *		<servlet-class>com.ss.web.servlet.CheckServlet</servlet-class>
 *	</servlet>
 *	
 *	<servlet-mapping>
 *		<servlet-name>checkservlet</servlet-name>
 *		<url-pattern>*.check</url-pattern>
 *	</servlet-mapping>
 * 
 */
public class CheckServlet extends HttpServlet {

    private static final long serialVersionUID = -8437797522934530082L;

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
            // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        HttpSession session = request.getSession();

     /**
     * @param width 图片宽
     * @param height 图片高
     * @param codeCount 字符个数
     * @param lineCount 干扰线条数
     */
        ValidateCode vCode = new ValidateCode(120, 40, 5, 100);
        session.setAttribute("code", vCode.getCode());
        vCode.write(response.getOutputStream());
    }
}
